package com.bidevalution.service.task;

import com.bidevalution.core.constant.ApiConstant;
import com.bidevalution.core.constant.CharConstant;
import com.bidevalution.service.TbFileService;
import com.bidevalution.service.ZbFileService;
import com.qtp.core.model.Response;
import com.qtp.core.util.OConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取项目列表，下载招标文件的xml和pdf
 * 每个项目列表开启一个线程，去检查投标文件
 * 开标结束之后，关闭对应的线程
 *
 * @author huyiqi
 * @date 2019/11/19
 */
@Component
public class SectionInfoTask {

    private static final Logger log = LoggerFactory.getLogger(SectionInfoTask.class);
    private static final Map<String, String> THREAD_MAPPING = new HashMap<>(16);

    public void getTenderInfo() {
        try {
            //获取招标项目列表
            Response<List<Map<String, Object>>> rsp =  zbFileService.getTenderList();
            if(rsp.isSuccess()) {
                List<Map<String, Object>> tenderList = rsp.getResult();
                if(tenderList != null) {
                    //遍历招标项目
                    for(Map<String, Object> tender:tenderList) {
                        String sectionCode = OConvertUtils.getString(tender.get("TenderNo"));
                        if(ignoreThisTender(sectionCode)) {
                            continue;
                        }
                        //下载招标文件
                        Response<String> zbRsp = zbFileService.downloadTenderFiles(tender);
                        if(zbRsp.isSuccess()) {
                            //开启线程，校验投标文件
                            if (!THREAD_MAPPING.containsKey(sectionCode)) {
                                log.info("准备开启线程，校验MD5");
                                THREAD_MAPPING.put(sectionCode, "1");
                                String sectionId = zbRsp.getResult();
                                tbFileService.getEnterFilePushed(sectionId, sectionCode);
                            }
                        } else {
                            log.error(zbRsp.getError());
                        }
                    }
                }
            } else {
                log.error("获取项目信息失败："+rsp.getError());
            }
        } catch (Exception e) {
            log.error("获取项目信息的定时任务出错", e);
        }
    }

    private boolean ignoreThisTender(String sectionCode) {
        if(StringUtils.isNotBlank(ApiConstant.BIND_SECTION_CODE)) {
            String[] bindSectionCodes = ApiConstant.BIND_SECTION_CODE.split(CharConstant.CHAR_ARRAY_SLASHER);
            List<String> codes = Arrays.asList(bindSectionCodes);
            //如果列表中不包含该标段编号，就忽略该标段
            return !codes.contains(sectionCode);
        }
        if(StringUtils.isNotBlank(ApiConstant.AREA_CODE)) {
            String[] areas = ApiConstant.AREA_CODE.split(CharConstant.CHAR_ARRAY_SLASHER);
            for(String area:areas) {
                if(sectionCode.startsWith(area)) {
                    //如果该标段属于配置的地区，就不忽略
                    return false;
                }
            }
            //如果该标段不属于配置的地区，就忽略
            return true;
        }
        //默认不忽略
        return false;
    }

    @Autowired
    private ZbFileService zbFileService;
    @Autowired
    private TbFileService tbFileService;
}
